﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZWL.Common;
using System.Text;

public partial class Main_MenuTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetQuanXian();
            Render();

        }
    }

    public void SetQuanXian()
    {
        //判断权限分配
        for (int i = 0; i < this.ListTreeView.Nodes.Count; i++)
        {
            for (int j = 0; j < this.ListTreeView.Nodes[i].ChildNodes.Count; j++)
            {
                //删除子菜单中的不在权限中的项
                for (int k = 0; k < this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                {
                    if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].Value + "|", ZWL.Common.PublicMethod.GetSessionValue("QuanXian")) == false)
                    {
                        this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k]);
                        k = -1;
                    }
                }
                //判断是父节点还是子节点
                if (this.ListTreeView.Nodes[i].ChildNodes[j].SelectAction == TreeNodeSelectAction.Expand)
                {
                    if (this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count <= 0)
                    {
                        this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                        j = -1;
                    }
                }
                else
                {
                    if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].Value + "|", ZWL.Common.PublicMethod.GetSessionValue("QuanXian")) == false)
                    {
                        this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                        j = -1;
                    }
                }
            }
            //判断是父节点还是子节点
            if (this.ListTreeView.Nodes[i].SelectAction == TreeNodeSelectAction.Expand)
            {
                if (this.ListTreeView.Nodes[i].ChildNodes.Count <= 0)
                {
                    this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                    i = -1;
                }
            }
            else
            {
                if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].Value + "|", ZWL.Common.PublicMethod.GetSessionValue("QuanXian")) == false)
                {
                    this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                    i = -1;
                }
            }
        }
    }

    protected  void Render()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<div id=\"body\" >\n");
        //sb.Append("<a id=\"expand_link\" href=\"javascript:menu_expand();\"><u><span id=\"expand_text\">展开</span></u></a>\n");
        sb.Append("<ul id=\"menu\">\n");
        sb.Append(RenderChild1(this.ListTreeView.Nodes));
        sb.Append("</ul>\n");
        sb.Append("</div>");
        this.body.InnerHtml = sb.ToString();
    }

    string RenderChild1(TreeNodeCollection nodes)
    {
        StringBuilder sb = new StringBuilder();
        foreach (TreeNode node in nodes)
        {
            //width=\"16\"  height=\"16\"
            sb.Append("<li class=\"L1\">");
            sb.Append("<a href=\"javascript:c(m" + node.Value + ");\" id=\"m" + node.Value + "\">");
            sb.Append("<span><img  src=\"" + node.ImageUrl + "\" align=\"absMiddle\" />" + node.Text + "</span>");
            sb.Append("</a>");
            sb.Append("</li>\n");
            if (node.ChildNodes.Count > 0)
            {

                sb.Append("<ul id=\"m" + node.Value + "d\" style=\"display: none;\" class=\"U1\">\n");
                sb.Append(RenderChild2(node.ChildNodes));
                sb.Append("</ul>\n");

            }
        }
        return sb.ToString();
    }

    string RenderChild2(TreeNodeCollection nodes)
    {
        StringBuilder sb = new StringBuilder();
        foreach (TreeNode node in nodes)
        {

            if (node.ChildNodes.Count == 0)
            {
                sb.Append("<li class=\"L22\">");
                sb.Append("<a href=\"javascript:a('" + node.NavigateUrl + "','" + node.Value + "');\" id=\"f" + node.Value + "\">");
                sb.Append("<span><img  src=\"" + node.ImageUrl + "\" align=\"absMiddle\" />" + node.Text + "</span>");
                sb.Append("</a>");
                sb.Append("</li>\n");
            }
            else
            {
                sb.Append("<li class=\"L21\">");
                sb.Append("<a href=\"javascript:c(f" + node.Value + ");\" id=\"f" + node.Value + "\">");
                sb.Append("<span><img  src=\"" + node.ImageUrl + "\" align=\"absMiddle\" />" + node.Text + "</span>");
                sb.Append("</a>");
                sb.Append("</li>\n");
                sb.Append("<ul id=\"f" + node.Value + "d\" style=\"display: none;\" >\n");
                sb.Append(RenderChild3(node.ChildNodes));
                sb.Append("</ul>\n");

            }
        }
        return sb.ToString();
    }

    string RenderChild3(TreeNodeCollection nodes)
    {
        StringBuilder sb = new StringBuilder();
        foreach (TreeNode node in nodes)
        {
            sb.Append("<li class=\"L3\">");
            sb.Append("<a href=\"javascript:a('" + node.NavigateUrl + "','" + node.Value + "');\" id=\"f" + node.Value + "\">");
            sb.Append("<span><img src=\"" + node.ImageUrl + "\" align=\"absMiddle\" />" + node.Text + "</span>");
            sb.Append("</a>");
            sb.Append("</li>\n");

        }
        return sb.ToString();
    }



}
